package com.dk.controller;


import com.alibaba.fastjson.JSONObject;
import com.dk.VO.TbUserVO;
import com.dk.api.OrdersFeignApi;
import com.dk.enums.ResultEnum;
import com.dk.result.Result;
import com.dk.service.TbAuctionService;
import com.dk.service.TbBailService;
import com.dk.service.TbBidrecordService;
import com.dk.util.JwtUtil;
import com.dk.vo.TbBailVO;
import com.dk.vo.TbBidrecordVO;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("TbAuction")
public class TbAuctionController {

    @Autowired
    private TbAuctionService tbAuctionService;
    @Autowired
    private TbBailService bailService;
    @Autowired
    private TbBidrecordService bidrecordService;
    @Autowired
    private JwtUtil jwtUtil;

    @Resource
    OrdersFeignApi ordersFeignApi;

    @Autowired
    private TbBailService tbBailService;

    //   展示拍卖车辆
    @PostMapping("show")
    @ApiOperation(value = "展示拍卖车辆")
    public Result show() {
        return Result.OK().setData(tbAuctionService.show());
    }

    @PostMapping("toshow")
    @ApiOperation(value = "进入到详情页")
    @ApiImplicitParam(name = "id", value = "车辆编号", required = true, type = "path")
    public Result toshow(Long id) {
        return Result.OK().setData(tbAuctionService.toshow(id));

    }

    @PostMapping("bidding")
    @ApiOperation(value = "参与竞拍")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "AuthToken", value = "登录令牌", required = true, type = "path"),
            @ApiImplicitParam(name = "cid", value = "车辆编号", required = true, type = "path"),
            @ApiImplicitParam(name = "money", value = "竞拍金额", required = true, type = "path")
    })
    public Result bidding(@RequestHeader("AuthToken") String AuthToken,@PathVariable Long cid,@PathVariable Long money) {
        try {
            //获取登录用户
            Claims claims = jwtUtil.parseJwt(AuthToken);
            TbUserVO userVO = JSONObject.parseObject(claims.getSubject(), TbUserVO.class);

            //根据uid查询bail表数据
            TbBailVO bailVO = bailService.selectBailByUid(userVO.getUserid());
            //判断用户是否支付定金
            if (bailVO.getBail() != null) {  //已支付
                TbBidrecordVO bidrecordVO = null;

                //本次出价要高于上次出价
                //根据cid查询price并且根据时间倒序
                List<TbBidrecordVO> bidrecordVOList = bidrecordService.selectPriceByCid(cid);
                //将money转换为BigDecimal类型
                BigDecimal bigDecimal = new BigDecimal(money);
                //获取最新出价，并和本次出价比较
                int flag = bigDecimal.compareTo(bidrecordVOList.get(0).getPrice());
                //flag = -1,表示bigdemical1小于bigdemical2；
                //flag = 0,表示bigdemical1等于bigdemical2；
                //flag = 1,表示bigdemical1大于bigdemical2；
                if (flag == 1) {
                    //出价
                    bidrecordVO = bidrecordService.bid(userVO.getUserid(), cid, money);
                }
                return Result.OK().setData(bidrecordVO);
            } else { //未支付
                return Result.respones(ResultEnum.BAIL_NOPAY);
            }
        } catch (Exception e) {
            return Result.ER();
        }
    }

    //报名 交保证金 不然无法进行竞价
    @PostMapping("bail")
    @ApiOperation(value = "参与竞拍")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "AuthToken", value = "登录令牌", required = true, type = "path"),
            @ApiImplicitParam(name = "cid", value = "车辆编号", required = true, type = "path"),
            @ApiImplicitParam(name = "num", value = "竞拍金额", required = true, type = "path")
    })
    public Result bail(@RequestHeader("AuthToken") String token, @PathVariable Long cid,@PathVariable Long num) {
        //    先去验证用户是否登录
        Claims claims = jwtUtil.parseJwt(token);
        TbUserVO user = JSONObject.parseObject(claims.getSubject(), TbUserVO.class);
        if (user == null) {
            return Result.respones(ResultEnum.NO_LOGIN);
        }
        //调用创建订单接口
        ordersFeignApi.pay(cid, token, num);

        String insert = tbBailService.insert(user.getUserid(), cid, num);
        if (insert.equals("1")) {
            return Result.ER();
        }

        return Result.OK();

    }

    @PostMapping("createAuctionOrder")
    @ApiOperation(value = "创建拍卖订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "AuthToken", value = "登录令牌", required = true, type = "path"),
            @ApiImplicitParam(name = "bid", value = "出价记录编号", required = true, type = "path")
    })
    public Result createAuctionOrder(@RequestHeader("AuthToken") String AuthToken,@PathVariable Long bid) {
        try {
            //    先去验证用户是否登录
            Claims claims = jwtUtil.parseJwt(AuthToken);
            TbUserVO user = JSONObject.parseObject(claims.getSubject(), TbUserVO.class);
            if (user == null) {
                return Result.respones(ResultEnum.NO_LOGIN);
            }
            TbBidrecordVO bidrecordVO = bidrecordService.selectAllByBid(bid);
            Long cid = bidrecordVO.getCid();
            BigDecimal price = bidrecordVO.getPrice();
            //将BigDecimal类型转为Long类型
            Long num = price.longValue();
            //调用创建订单接口
            ordersFeignApi.pay(cid, AuthToken, num);
            return Result.OK();
        } catch (Exception e) {
            return Result.ER();
        }
    }
}
